package id.menu;

import id.game.NewGame;
import id.utils.Globals;

import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.FengGUIOptional;
import org.fenggui.Widget;
import org.fenggui.actor.SimpleWindowPositioningActor;
import org.fenggui.binding.render.Binding;
import org.fenggui.binding.render.Graphics;
import org.fenggui.composite.MessageWindow;
import org.fenggui.composite.menu.Menu;
import org.fenggui.composite.menu.MenuBar;
import org.fenggui.composite.menu.MenuItem;
import org.fenggui.event.DisplayResizedEvent;
import org.fenggui.event.IDisplayResizedListener;
import org.fenggui.event.IMenuItemPressedListener;
import org.fenggui.event.MenuItemPressedEvent;
import org.fenggui.layout.StaticLayout;
import org.fenggui.theme.DefaultTheme;
import org.fenggui.theme.ITheme;
import org.fenggui.theme.XMLTheme;
import org.fenggui.util.Point;
import org.fenggui.util.Spacing;


public class MainMenu implements IOption{
	
	private Display display = null;

	public MainMenu() {

	}

	public void registerExample(final IOption option, Menu parent, boolean activate) {
		MenuItem item = new MenuItem(option.getOptionName(), parent.getAppearance());
		parent.addItem(item);
		item.setEnabled(activate);
		item.addMenuItemPressedListener(new IMenuItemPressedListener() {
			public void menuItemPressed(MenuItemPressedEvent menuItemPressedEvent) { 
				buildOptionGUIinRenderThread(option);
			}

		});
	}

	/**
	 * Exemplo de um menu
	 * 
	 * @param menuBar
	 */
	
//	private void buildTextMenu(MenuBar menuBar) {
//		Menu textMenu = FengGUI.createMenu(menuBar, "Text", false);
//
//		registerExample(new TextFieldExample(), textMenu, true);
//		registerExample(new TextAreaExample(), textMenu, true);
//		registerExample(new TextViewExample(), textMenu, true);
//		registerExample(new MultiLineLabelExample(), textMenu, true);
//		// registerExample(new LabelExample(), textMenu, true);
//		registerExample(new FontExample(), textMenu, true);
//	}

	private void buildHelpMenu(MenuBar menuBar) {
		Menu helpMenu = FengGUI.createMenu(menuBar, "Help", false);
		MenuItem about = new MenuItem("About", menuBar.getAppearance());
		helpMenu.addItem(about);

		about.addMenuItemPressedListener(new IMenuItemPressedListener() {

			public void menuItemPressed(
					MenuItemPressedEvent menuItemPressedEvent) {
				String t = "Industry Dwarfs version " + Globals.getGameVersion() + "\n" + Globals.getGameWeb();
				MessageWindow mw = new MessageWindow(t);
				mw.setTitle("About");
				mw.updateMinSize();
				mw.setSize(mw.getMinWidth() + 100, mw.getMinHeight());
				mw.layout();
				display.addWidget(mw);
				StaticLayout.center(mw, display);
			}

		});
	}

	private void buildProgramMenu(final MenuBar menuBar) {
		Menu programMenu = FengGUI.createMenu(menuBar, "Program", false);
		registerExample(new NewGame(), programMenu, true);
		MenuItem exitItem = new MenuItem("Exit", menuBar.getAppearance());
		programMenu.addItem(exitItem);
		
		// register "Exit" item to quit the app
		exitItem.addMenuItemPressedListener(new IMenuItemPressedListener() {
			public void menuItemPressed(
					MenuItemPressedEvent menuItemPressedEvent) {
				quit();
			}
		});
	}

	private void buildMenuBar() {

		final MenuBar menuBar = FengGUI.createMenuBar(display);

		buildProgramMenu(menuBar);
		buildHelpMenu(menuBar);

		// position MenuBar in Display
		menuBar.updateMinSize(); // we have not layouted anything yet...
		menuBar.setX(0);
		menuBar.setY(display.getHeight() - menuBar.getMinHeight());
		menuBar.setSize(display.getWidth(), menuBar.getMinHeight());
		menuBar.setShrinkable(false);

		// if the OpenGL screen is resized, put the MenuBar back in place
		Binding.getInstance().addDisplayResizedListener(
				new IDisplayResizedListener() {

					public void displayResized(
							DisplayResizedEvent displayResizedEvent) {
						menuBar.setX(0);
						menuBar.setY(displayResizedEvent.getHeight()
								- menuBar.getMinHeight());
						menuBar.setSize(displayResizedEvent.getWidth(), menuBar
								.getMinHeight());
					}

				});
	}

	public void quit() {
		System.exit(0);
	}

	private void buildOptionGUIinRenderThread(final IOption option) {
		Widget w = new Widget() {
			public void paint(Graphics g) {
				display.removeWidget(this);
				option.buildGUI(display);
			}
		};
		display.addWidget(w);
	}

	private void loadThemeDirect(final String filename, final boolean noTheme) {
		long time = System.currentTimeMillis();
		ITheme theme = null;

		if (!noTheme) {
			if (filename == null)
				theme = new DefaultTheme();
			else {
				try {
					theme = new XMLTheme(filename);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		FengGUI.setTheme(theme);
		FengGUI.initPrototypes();
		FengGUIOptional.initOptional();

		long tookTime = System.currentTimeMillis() - time;
		System.out.println("FengGUI Theme Loading: " + tookTime);
		/*
		 * FengGUI.setUpAppearance(helpMenu); FengGUI.setUpAppearance(miscMenu);
		 * FengGUI.setUpAppearance(themeMenu);
		 * FengGUI.setUpAppearance(widgetMenu);
		 * FengGUI.setUpAppearance(programMenu);
		 * FengGUI.setUpAppearance(nonsenseMenu);
		 * FengGUI.setUpAppearance(compositesMenu);
		 * FengGUI.setUpAppearance(containersMenu);
		 */
	}

	public void buildGUI(Display g) {
		display = g;
		loadThemeDirect("data/themes/QtCurve/QtCurve.xml", false);
		buildMenuBar();
		display.layout();
		SimpleWindowPositioningActor actor = new SimpleWindowPositioningActor(
				new Point(10, -25), new Spacing(20, 5, 5, 5));
		actor.hook(display);
	}

	public String getOptionName() {
		return "Test Almost Everything";
	}

	public String getOptionDescription() {
		return "Shows almost every Widget in FengGUI";
	}

}
